package com.pn.controller;

import com.pn.dto.AssignRoleDto;
import com.pn.entity.Result;
import com.pn.entity.User;
import com.pn.model.Role;
import com.pn.model.UserInfo;
import com.pn.page.Page;
import com.pn.service.RoleService;
import com.pn.service.UserInfoService;
import com.pn.utils.CurrentUser;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * ClassName:UserInfoController
 * Package:com.pn.controller
 * Description:
 *
 * @date:2024/12/28 19:07
 * @author:bailihua
 */
@RequestMapping("/user")
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询用户信息
     * User比自动生成的UserInfo多加了一些东西，所以先用他们的
     * @param page
     * @param user
     * @return
     */
    @RequestMapping("/user-list")
    public Result userList(Page page, User user){
        //执行业务
        page = userInfoService.queryUserByPage(page, user);

        return Result.ok(page);
    }

    /**
     * 添加用户
     * @return
     */
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody UserInfo userInfo, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前登录的用户Id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();
        userInfo.setCreateBy(createBy);

        //执行业务
        Result result = userInfoService.saveUser(userInfo);

        //响应
        return result;

    }

    /**
     * 修改用户状态
     * @return
     */
    @RequestMapping("/updateState")
    public Result updateUserState(@RequestBody UserInfo userInfo){
        //执行业务
        Result result = userInfoService.setUserState(userInfo);

        //响应
        return result;

    }

    /**
     * 查询用户已经被分配的角色
     * @return
     */
    @RequestMapping("/user-role-list/{userId}")
    public  Result userRoleList(@PathVariable Integer userId){
        //执行业务
        List<Role> roleList =  roleService.queryUserRoleByUid(userId);

        //响应
        return Result.ok(roleList);
    }

    /**
     * 给用户分配角色
     * @return
     */
    @RequestMapping("/assignRole")
    public Result addignRole(@RequestBody AssignRoleDto assignRoleDto){
        //执行业务
        userInfoService.assignRole(assignRoleDto);
        //响应
        return Result.ok("分配角色成功");
    }

    /**
     * 根据用户Id删除单个用户
     * @param userId
     * @return
     */
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUserById(@PathVariable Integer userId){
        //执行业务
        Result result =userInfoService.removeUserByIds(Arrays.asList(userId));
        //响应
        return result;
    }

    @RequestMapping("/deleteUserList")
    public Result deleteUserByIds(@RequestBody List<Integer> userIdList){
        //执行业务
        Result result = userInfoService.removeUserByIds(userIdList);
        //响应
        return result;
    }

    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody UserInfo userInfo,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){

        //拿到当前登录用户的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int updateBy = currentUser.getUserId();

        userInfo.setUpdateBy(updateBy);

        //执行业务
        Result result = userInfoService.setUserById(userInfo);
        //响应
        return result;
    }
    @RequestMapping("updatePwd/{userId}")
    public Result resetPassword(@PathVariable Integer userId){
        //执行业务
        Result result = userInfoService.setPwdById(userId);
        //响应
        return result;

    }


}
